﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        private void tsbCreate_Click(object sender, EventArgs e)
        {
            Form2 frm = new Form2();
            frm.TV = treeView1;
            frm.Show();
        }

        private void tsbDisabled_Click(object sender, EventArgs e)
        {
            //foreach (TreeNode tn in treeView1.Nodes)
            //{
            //    RecursiveTree(tn, false);
            //}
            //test(false);
            // 调用普通遍历（深度优先）函数禁用TextBox控件
            RecursiveTree(treeView1.Nodes, false);
            // todo:与tsbEnabledOrDisable_Click联动
        }


        private void tsbEnabled_Click(object sender, EventArgs e)
        {
            //test(true);
            // 调用普通遍历（深度优先）函数启用TextBox控件
            RecursiveTree(treeView1.Nodes, true);
        }



        private void tsbEnabledOrDisable_Click(object sender, EventArgs e)
        {
            // 调用广度优先遍历函数禁用TextBox控件
            int i = RecursiveTreeBFS(treeView1.Nodes);
            if (i == 0)
            {
                // this是FormMain
                // this.Text = "启用(BFS)";
                ((ToolStripButton)sender).Text = "启用(BFS)";
            }
            else
            {
                // this.Text = "禁用(BFS)";
                ((ToolStripButton)sender).Text = "禁用(BFS)";
            }

        }

        //void test(bool enabled)
        //{
        //    foreach (TreeNode tn in treeView1.Nodes)
        //    {
        //        RecursiveTree(tn, enabled);
        //    }
        //}

        void RecursiveTree(TreeNodeCollection tnc, bool enabled)
        {
            if (tnc != null)
            {
                for (int i = 0; i < tnc.Count; i++)
                {
                    TreeNode tnChild = tnc[i];
                    if (tnChild.Tag is TextBox)
                    {
                        TextBox tb = tnChild.Tag as TextBox;
                        tb.Enabled = enabled;
                    }
                    RecursiveTree(tnChild.Nodes, enabled);
                }
            }
        }

        // 遍历控件（BFS广度优先）
        int RecursiveTreeBFS(TreeNodeCollection tnc)
        {
            Queue<TreeNode> queue = new Queue<TreeNode>();
            int flag = 0;   // 默认返回tb.Enabled的状态为0，即tb.Enabled=false
            if (tnc != null)
            {
                // 使用这个for循环，tnc可能会有根节点
                for (int i = 0; i < tnc.Count; i++)
                {
                    // 先将所有根节点加入到队列
                    queue.Enqueue(tnc[i]);
                }
                TreeNode tnChild;
                while (queue.Count > 0)
                {
                    // 取出队列中的第一个节点
                    tnChild = queue.Dequeue();
                    // 将tnChild节点的所有子节点加入到队列
                    for (int i = 0; i < tnChild.Nodes.Count; i++)
                    {
                        queue.Enqueue(tnChild.Nodes[i]);
                    }
                    // 功能代码，如果 tnChild.Tag is TextBox，则更换它的Enabled属性值（）
                    if (tnChild.Tag is TextBox)
                    {
                        TextBox tb = tnChild.Tag as TextBox;
                        // 取反，实现启用或禁用相互切换
                        tb.Enabled = !tb.Enabled;
                        if (tb.Enabled) flag = 1;
                    }

                }
            }
            return flag;
        }
    }
}
